<?php
uses('model');

class wishlistModel extends Model {

    function __construct() {
        parent::__construct();
    }

    function wishlistList($v) {
        $this->dbconnect();

        //指定用户时
        if ($v['iid']) {
            if ($v['uid'] = $this->db->GetOne($this->table['users'], 'id', "username=".$this->db->qstr($v['iid']))) {
                $data['uid'] = $v['uid'];
                if ($v['pagesize']) {
                    $data['list'] = $this->db->GetPage(array("{$this->table['wishlist']} a LEFT JOIN {$this->table['items']} b ON a.iid = b.id", 'b.id, b.name, b.price1', "a.uid={$v['uid']} AND b.stocknum>0 ORDER BY ". ($v['sort'] ? $v['sort'] : 'id') ." ".  ($v['sortorder'] ? $v['sortorder'] : 'DESC')), $data['page'], $v['pagesize']);
                } else {
                    $data['list'] = $this->db->GetAll("{$this->table['wishlist']} a LEFT JOIN {$this->table['items']} b ON a.iid = b.id", 'b.id, b.name, b.price1', "a.uid={$v['uid']} AND b.stocknum>0 ORDER BY a.". ($v['sort'] ? $v['sort'] : 'id') ." ".  ($v['sortorder'] ? $v['sortorder'] : 'DESC') );
                }
            } else {
                $data['code']['wishlistList'] = 4;
            }
        } else if ($v['search']) { //指定搜索条件时
            $fields = 'a.uid, a.updated, b.username, b.email, c.id, c.name, c.price1, c.name, c.price1';
            $data['search'] = $v['search'];

            $s_f[] = 'b.username';
            $s_f[] = 'b.truename';
            $s_f[] = 'b.email';
            $s_f[] = 'c.name';

            $s_str = $this->makelikequery($v['search'], $s_f, $this->db);

            if ($v['pagesize']) {
                $data['list'] = $this->db->GetPage(array("{$this->table['wishlist']} a LEFT JOIN {$this->table['users']} b ON a.uid = b.id LEFT JOIN {$this->table['items']} c ON a.iid=c.id", $fields, $s_str." ORDER BY ". ($v['sort'] ? $v['sort'] : 'id') ." ".  ($v['sortorder'] ? $v['sortorder'] : 'DESC')), $data['page'], $v['pagesize']);
            } else {
                $data['list'] = $this->db->GetAll( "{$this->table['wishlist']} a LEFT JOIN {$this->table['users']} b ON a.uid = b.id LEFT JOIN {$this->table['items']} c ON a.iid=c.id", $fields, $s_str." ORDER BY ". ($v['sort'] ? $v['sort'] : 'id') ." ".  ($v['sortorder'] ? $v['sortorder'] : 'DESC') );
            }
        } else {
            $data['code']['wishlistList'] = 4;
        }

        return $data;
    }

    function newWishList($v) {
        $this->dbconnect();

        $fields['iid']      = $v['iid'];
        $fields['uid']      = $v['uid'];
        $fields['updated']  = time();

        //检查物品是否存在
        if ($this->db->Count($this->table['items'], "id={$v['iid']}")) {
            //重复则更新时间
            if ($id = $this->db->GetOne($this->table['wishlist'], 'id', "iid={$v['iid']} AND uid=".$fields['uid'])) {
                if ($v['remove'])
                    $data['code']['newWishList'] = $this->delWishlist($v);
                else
                    $data['code']['newWishList'] = $this->db->Update($this->table['wishlist'], $fields, "id=$id") ? 4 : 2;
            } else {
                $data['code']['newWishList'] = $this->db->Insert($this->table['wishlist'], $fields) ? 1 : 2;
            }
        } else {
            $data['code']['newWishList'] = 64;
        }

        return $data;
    }

    function delWishList($v) {
        $this->dbconnect();

        return $data['code']['delComment'] = $this->db->Delete($this->table['wishlist'], "iid={$v['iid']} && uid={$v['uid']}") ? 8 : 2;
    }

    function isUserWish($iid, $uid) {
        $this->dbconnect();

        return $this->db->GetOne($this->table['wishlist'], 'id', "uid=$uid AND iid=$iid");
    }

    function wishRequest($v) {
        $this->dbconnect();

        if ($data['uid']        = $this->db->GetOne($this->table['wishlist'], 'uid', "iid={$v['iid']} AND md5(uid) = ".$this->db->qstr($v['md5']))) {
            $data['info']       = $this->db->GetRow($this->table['users'], 'username,truename,tel,address,zipcode', "id={$data['uid']}");
            $data['username']   = $data['info']['username'];
        } else {
            $data['code']['wishRequest'] = 2;
        }

        return $data;
    }
}
?>